home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gp_ntfs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  6.6 KB  |  227 lines

  1. /* Copyright (C) 1992, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gp_ntfs.c,v 1.4 2000/09/19 19:00:24 lpd Exp $ */
  20. /* file system stuff for MS-Windows WIN32 and MS-Windows NT */
  21. /* hacked from gp_dosfs.c by Russell Lang */
  22.  
  23. #include "stdio_.h"
  24. #include <fcntl.h>
  25. #include <io.h>
  26. #include <stdio.h>
  27. #include "dos_.h"
  28. #include "memory_.h"
  29. #include "string_.h"
  30. #include "gstypes.h"
  31. #include "gsmemory.h"
  32. #include "gsstruct.h"
  33. #include "gp.h"
  34. #include "gsutil.h"
  35. #include "windows_.h"
  36.  
  37. /* ------ Printer accessing ------ */
  38.  
  39. /* Put a printer file (which might be stdout) into binary or text mode. */
  40. /* This is not a standard gp procedure, */
  41. /* but all MS-DOS configurations need it. */
  42. private int
  43. setmode_binary(int fno, bool binary)
  44. {
  45.     /* Use non-standard setmode that almost all NT compilers offer. */
  46. #if defined(__STDC__) && !defined(__WATCOMC__)
  47.     return _setmode(fno, binary ? _O_BINARY : _O_TEXT);
  48. #else
  49.     return setmode(fno, binary ? O_BINARY : O_TEXT);
  50. #endif
  51. }
  52. void
  53. gp_set_file_binary(int prnfno, int binary)
  54. {
  55.     DISCARD(setmode_binary(prnfno, binary != 0));
  56. }
  57.  
  58. /* ------ File accessing -------- */
  59.  
  60. /* Set a file into binary or text mode. */
  61. int
  62. gp_setmode_binary(FILE * pfile, bool binary)
  63. {
  64.     /* Use non-standard fileno that almost all NT compilers offer. */
  65. #if defined(__STDC__) && !defined(__WATCOMC__)
  66.     int code = setmode_binary(_fileno(pfile), binary);
  67. #else
  68.     int code = setmode_binary(fileno(pfile), binary);
  69. #endif
  70.  
  71.     return (code == -1 ? -1 : 0);
  72. }
  73.  
  74. /* ------ File names ------ */
  75.  
  76. /* Define the character used for separating file names in a list. */
  77. const char gp_file_name_list_separator = ';';
  78.  
  79. /* Define the string to be concatenated with the file mode */
  80. /* for opening files without end-of-line conversion. */
  81. const char gp_fmode_binary_suffix[] = "b";
  82.  
  83. /* Define the file modes for binary reading or writing. */
  84. const char gp_fmode_rb[] = "rb";
  85. const char gp_fmode_wb[] = "wb";
  86.  
  87. /* Answer whether a file name contains a directory/device specification, */
  88. /* i.e. is absolute (not directory- or device-relative). */
  89. bool
  90. gp_file_name_is_absolute(const char *fname, uint len)
  91. {                /* A file name is absolute if it contains a drive specification */
  92.     /* (second character is a :) or if it start with 0 or more .s */
  93.     /* followed by a / or \. */
  94.     if (len >= 2 && fname[1] == ':')
  95.     return true;
  96.     while (len && *fname == '.')
  97.     ++fname, --len;
  98.     return (len && (*fname == '/' || *fname == '\\'));
  99. }
  100.  
  101. /* Answer the string to be used for combining a directory/device prefix */
  102. /* with a base file name.  The file name is known to not be absolute. */
  103. const char *
  104. gp_file_name_concat_string(const char *prefix, uint plen,
  105.                const char *fname, uint len)
  106. {
  107.     if (plen > 0)
  108.     switch (prefix[plen - 1]) {
  109.         case ':':
  110.         case '/':
  111.         case '\\':
  112.         return "";
  113.     };
  114.     return "\\";
  115. }
  116.  
  117. /* ------ File enumeration ------ */
  118.  
  119. struct file_enum_s {
  120.     WIN32_FIND_DATA find_data;
  121.     HANDLE find_handle;
  122.     char *pattern;        /* orig pattern + modified pattern */
  123.     int patlen;            /* orig pattern length */
  124.     int pat_size;        /* allocate space for pattern */
  125.     int head_size;        /* pattern length through last */
  126.     /* :, / or \ */
  127.     int first_time;
  128.     gs_memory_t *memory;
  129. };
  130. gs_private_st_ptrs1(st_file_enum, struct file_enum_s, "file_enum",
  131.             file_enum_enum_ptrs, file_enum_reloc_ptrs, pattern);
  132.  
  133. /* Initialize an enumeration.  Note that * and ? in a directory */
  134. /* don't work, and \ is taken literally unless a second \ follows. */
  135. file_enum *
  136. gp_enumerate_files_init(const char *pat, uint patlen, gs_memory_t * mem)
  137. {
  138.     file_enum *pfen = gs_alloc_struct(mem, file_enum, &st_file_enum, "gp_enumerate_files");
  139.     int pat_size = 2 * patlen + 1;
  140.     char *pattern;
  141.     int hsize = 0;
  142.     int i;
  143.  
  144.     if (pfen == 0)
  145.     return 0;
  146.  
  147.     /* pattern could be allocated as a string, */
  148.     /* but it's simpler for GC and freeing to allocate it as bytes. */
  149.  
  150.     pattern = (char *)gs_alloc_bytes(mem, pat_size,
  151.                      "gp_enumerate_files(pattern)");
  152.     if (pattern == 0)
  153.     return 0;
  154.     memcpy(pattern, pat, patlen);
  155.     /* find directory name = header */
  156.     for (i = 0; i < patlen; i++) {
  157.     switch (pat[i]) {
  158.         case '\\':
  159.         if (i + 1 < patlen && pat[i + 1] == '\\')
  160.             i++;
  161.         /* falls through */
  162.         case ':':
  163.         case '/':
  164.         hsize = i + 1;
  165.     }
  166.     }
  167.     pattern[patlen] = 0;
  168.     pfen->pattern = pattern;
  169.     pfen->patlen = patlen;
  170.     pfen->pat_size = pat_size;
  171.     pfen->head_size = hsize;
  172.     pfen->memory = mem;
  173.     pfen->first_time = 1;
  174.     memset(&pfen->find_data, 0, sizeof(pfen->find_data));
  175.     pfen->find_handle = INVALID_HANDLE_VALUE;
  176.     return pfen;
  177. }
  178.  
  179. /* Enumerate the next file. */
  180. uint
  181. gp_enumerate_files_next(file_enum * pfen, char *ptr, uint maxlen)
  182. {
  183.     int code = 0;
  184.     uint len;
  185.  
  186.     if (pfen->first_time) {
  187.     pfen->find_handle = FindFirstFile(pfen->pattern, &(pfen->find_data));
  188.     if (pfen->find_handle == INVALID_HANDLE_VALUE)
  189.         code = -1;
  190.     pfen->first_time = 0;
  191.     } else {
  192.     if (!FindNextFile(pfen->find_handle, &(pfen->find_data)))
  193.         code = -1;
  194.     }
  195.     if (code != 0) {        /* All done, clean up. */
  196.     gp_enumerate_files_close(pfen);
  197.     return ~(uint) 0;
  198.     }
  199.     len = strlen(pfen->find_data.cFileName);
  200.  
  201.     if (pfen->head_size + len < maxlen) {
  202.     memcpy(ptr, pfen->pattern, pfen->head_size);
  203.     strcpy(ptr + pfen->head_size, pfen->find_data.cFileName);
  204.     return pfen->head_size + len;
  205.     }
  206.     if (pfen->head_size >= maxlen)
  207.     return 0;        /* no hope at all */
  208.  
  209.     memcpy(ptr, pfen->pattern, pfen->head_size);
  210.     strncpy(ptr + pfen->head_size, pfen->find_data.cFileName,
  211.         maxlen - pfen->head_size - 1);
  212.     return maxlen;
  213. }
  214.  
  215. /* Clean up the file enumeration. */
  216. void
  217. gp_enumerate_files_close(file_enum * pfen)
  218. {
  219.     gs_memory_t *mem = pfen->memory;
  220.  
  221.     if (pfen->find_handle != INVALID_HANDLE_VALUE)
  222.     FindClose(pfen->find_handle);
  223.     gs_free_object(mem, pfen->pattern,
  224.            "gp_enumerate_files_close(pattern)");
  225.     gs_free_object(mem, pfen, "gp_enumerate_files_close");
  226. }
  227.